package com.house365.core.task;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;

import com.house365.core.R;
import com.house365.core.application.BaseApplication;
import com.house365.core.bean.common.CommonTaskInfo;
import com.house365.core.constant.CorePreferences;
import com.house365.core.http.exception.HtppApiException;
import com.house365.core.http.exception.HttpParseException;
import com.house365.core.http.exception.NetworkUnavailableException;
import com.house365.core.util.ActivityUtil;
import com.house365.core.view.LoadingDialog;

/**
 * @author py
 * @date 2012-6-5
 * @comment
 */
public abstract class CommonAsyncTask<V> extends
		AsyncTask<Object, Object, Object> {
	protected LoadingDialog loadingDialog;
	protected Context context;
	protected BaseApplication mApplication;
	protected int loadingresid;

	public CommonAsyncTask(Context context) {
		this.context = context;
		try{
			mApplication = (BaseApplication) ((Activity) context).getApplication();
		}catch(ClassCastException e){
			mApplication = (BaseApplication) context;
		}
		
	}

	public CommonAsyncTask(Context context, int resid) {
		this.context = context;
		this.loadingresid = resid;
		try{
			mApplication = (BaseApplication) ((Activity) context).getApplication();
		}catch(ClassCastException e){
			mApplication = (BaseApplication) context;
		}
	}

	public LoadingDialog getLoadingDialog() {
		if (loadingDialog == null) {
			loadingDialog = new LoadingDialog(this.context, R.style.dialog,
					loadingresid);
			loadingDialog.setCancelable(true);
			loadingDialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					// TODO Auto-generated method stub
					onDialogCancel();
				}
			});
		}
		return loadingDialog;
	}

	public void setLoadingDialog(LoadingDialog loadingDialog) {
		this.loadingDialog = loadingDialog;
	}

	public int getLoadingresid() {
		return loadingresid;
	}

	public void setLoadingresid(int loadingresid) {
		this.loadingresid = loadingresid;
	}

	protected void onPreExecute() {
		if (loadingresid > 0 && getLoadingDialog()!=null) {
			getLoadingDialog().show();
		}
	}

	@Override
	protected CommonTaskInfo<V> doInBackground(Object... params) {
		CommonTaskInfo<V> commonTaskInfo = new CommonTaskInfo<V>();
		commonTaskInfo.setResult(CommonTaskInfo.SUCCESS);
		V v = null;
		try {
			v = onDoInBackgroup();
		} catch (NetworkUnavailableException e) {
			CorePreferences.ERROR(e);
			commonTaskInfo.setResult(CommonTaskInfo.NETWORK_UNAVAILABLE);
		} catch (HtppApiException e) {
			CorePreferences.ERROR(e);
			commonTaskInfo.setResult(CommonTaskInfo.HTTP_REQUEST_ERROR);
		} catch (HttpParseException e) {
			CorePreferences.ERROR(e);
			commonTaskInfo.setResult(CommonTaskInfo.PARSE_ERROR);
		}
		commonTaskInfo.setData(v);
		return commonTaskInfo;
	}

	@Override
	protected void onPostExecute(Object result) {
		if (loadingresid > 0) {
			getLoadingDialog().dismiss();
		}
		if (context instanceof Activity && ((Activity) context).isFinishing()) {
			return;
		}
		CommonTaskInfo<V> commonTaskInfo = (CommonTaskInfo<V>) result;
		if (commonTaskInfo.getResult() == CommonTaskInfo.NETWORK_UNAVAILABLE) {
			onNetworkUnavailable();
			return;
		}
		if (commonTaskInfo.getResult() == CommonTaskInfo.HTTP_REQUEST_ERROR) {
			onHttpRequestError();
			return;
		}
		if (commonTaskInfo.getResult() == CommonTaskInfo.PARSE_ERROR) {
			onParseError();
			return;
		}
		onAfterDoInBackgroup(commonTaskInfo.getData());
	}

	public abstract void onAfterDoInBackgroup(V r);

	public abstract V onDoInBackgroup() throws NetworkUnavailableException,
			HtppApiException, HttpParseException;
	/**
	 * 当用户取消加载框的时候调用，前提打开加载框
	 */
	protected void onDialogCancel() {

	} 
	/**
	 *当网络不可用时候调用
	 */
	protected void onNetworkUnavailable() {
		ActivityUtil.showToast(context, R.string.text_network_unavailable);
	}
	/**
	 * 当http请求发生错误时调用，包括接口错误，请求超时
	 */
	protected void onHttpRequestError() {
		ActivityUtil.showToast(context, R.string.text_http_request_error);
	}
	/**
	 * 当数据解析异常时调用
	 */
	protected void onParseError() {
		ActivityUtil.showToast(context, R.string.text_http_parse_error);
	}
}
